package model;

import constants.SMSConstants;
import Repository.Repository;
import Repository.RepositoryFactory;

public class Quoted implements SMSObjectState{

	private final String NAME = "Quoted";
	RuleSelector ruleSelector;
	NextStateCalculator stateCalculator;
	Repository repository;
	@Override
	public void execute(SMSObject smsObj) {
		if(smsObj instanceof Opportunity){
			repository = RepositoryFactory.getRepository(SMSConstants.OPPORTUNITY);
			ruleSelector =  new ApproveRuleSelector(smsObj);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,ruleSelector);
			//System.out.println("New state..." + newState.getStateName());
			
			if(newState.getStateName().equals("Imported")){
				System.out.println("****Opportunity  cannot approved as it doesnot pass the rules****");
				smsObj.setCurrentState(newState);
				repository.update(smsObj.getSmsObjId(), smsObj);
				return;
				
			}
			//System.out.println("Opportunity" + smsObj.getSmsObjId() +" has been assigned");
			smsObj.setCurrentState(newState);
			
			repository.update(smsObj.getSmsObjId(), smsObj);	
			System.out.println("Successfully approved quote");
		}
				
	}

	@Override
	public String getStateName() {
		return NAME;
	}

}
